package com.dst.controller;

import com.dst.dao.BrandMapper;
import com.dst.entity.Brand;
import com.dst.entity.Phone;
import com.dst.service.BrandService;
import com.dst.service.PhoneService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/phone")
public class PhoneController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private PhoneService phoneService;
  /*  @RequestMapping("/selectAllController")
    public String selectAllController(Model model){
        List<Phone> phones = phoneService.selectAll();
        model.addAttribute("phones",phones);
        return "/phone-list.jsp";
    }*/
    @RequestMapping("/deleteByIdPhoneController")
    public String deleteByIdPhoneController(int id){
        phoneService.delete(id);
        return "redirect:/phone/selectAllController.do";
    }
    @RequestMapping("/selectByIdPhoneController")
    public String selectByIdPhoneController(int id,Model model){
        Phone phone = phoneService.selectById(id);
        model.addAttribute("phone",phone);
        List<Brand> brands = brandService.selectAll();
        model.addAttribute("brands",brands);
        return "/updatephone.jsp";
    }
    @RequestMapping("/updatePhoneController")
    public String updatePhoneController(Phone phone){
        phoneService.update(phone);
        return "redirect:/phone/selectAllController.do";
    }
    @RequestMapping("addPhoneController")
    public String addPhoneController(Phone phone){
        phoneService.insert(phone);
        return "redirect:/phone/selectAllController.do";
    }
    @RequestMapping("/batchDeleteController")
    public String batchDeleteController(int[] ids){

        phoneService.deleteBatch(ids);
        return "redirect:/phone/selectAllController.do";
    }
@RequestMapping("/selectAllController")
    public String selectAllController(String curpage,Model model){
        int cupage=1;
        if(curpage!=null){
            cupage=Integer.parseInt(curpage);
        }
        int pagesize=2;
    PageInfo<Phone> phones = phoneService.selectLimitAll(cupage, pagesize);
    model.addAttribute("phones",phones);
    return "/phone-list.jsp";
}
}
